package br.com.deployment.mobile;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import br.com.deployment.mobile.entities.Issues;
import br.com.deployment.mobile.entities.Options;
import br.com.deployment.mobile.entities.Questionnaire;
import br.com.deployment.mobile.util.Teste;

public class GenerateQuestionaire extends Activity {

	private LinearLayout linearLayout;
	private EditText editTextIssues;
	private TextView textView;
	private int id;
	private int max;
	private List<Issues> issues;
	private Questionnaire questionnaire;
	
	private List<Issues> issuesResponse;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_generate_questionaire);

		linearLayout = (LinearLayout) findViewById(R.id.generate_linear_layout);

		editTextIssues = (EditText) linearLayout
				.findViewById(R.id.editText_issues);
		textView = ((TextView) findViewById(R.id.textView_issues));

		Button buttonN = (Button) findViewById(R.id.send_button_next_issues);
		buttonN.setOnClickListener(buttonNext);
		
		Button buttonP = (Button) findViewById(R.id.send_button_previous_issues);
		buttonP.setOnClickListener(buttonPrevious);
		
		questionnaire = Teste.getExample().get(0);
		generateIssues();
		
		issuesResponse = new ArrayList<Issues>();
	}

	public void generateIssues() {
		issues = questionnaire.getIssueses();
		max = issues.size();
		issues(issues.get(0));

	}

	public void issues(Issues issues) {
		linearLayout.removeAllViews();
		linearLayout.addView(textView);

		textView.setText(issues.getTitulo());

		switch (issues.getTypeIssues()) {
		case CHOICEMULTIPLE:
			choiceMultiple(issues.getOptions());
			break;
		case OPEN:
			open();
			break;
		case CHOOSEONE:
			chooseOne(issues.getOptions());
			break;
		}
	}

	public void choiceMultiple(List<Options> ops) {
		for (Options op : ops) {
			CheckBox checkBox = new CheckBox(this);
			checkBox.setText(op.getTitulo());
			linearLayout.addView(checkBox);
		}
	}

	public void chooseOne(List<Options> ops) {
		RadioGroup radioGroup = new RadioGroup(this);
		for (Options op : ops) {
			RadioButton radioButton = new RadioButton(this);
			radioButton.setText(op.getTitulo());

			radioGroup.addView(radioButton);
		}
		linearLayout.addView(radioGroup);
	}

	public void open() {
		linearLayout.addView(editTextIssues);
		editTextIssues.setVisibility(1);
	}

	OnClickListener buttonNext = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if (id < max - 1) {
				id++;
				issues(issues.get(id));
			}else{
				conclusionIssues();
			}
		}
	};
	
	OnClickListener buttonPrevious = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if (id >= 0) {
				id--;
				issues(issues.get(id));
			}
		}
	};
	
	public void onRadioButtonClicked(View view) {
	    boolean checked = ((RadioButton) view).isChecked();
	    
//	    switch(view.getId()) {
//	        case R.id.radio_pirates:
//	            if (checked)
//	            	
//	            break;
//	        case R.id.radio_ninjas:
//	            if (checked)
//	            	
//	            break;
//	    }
	}

	private void showMessage(String msg) {
		Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
	}

	private void conclusionIssues(){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Deseja enviar relatório ? ")
	       .setTitle("Concluido");
		builder.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		           }
		       });
		builder.setNegativeButton("Não", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		           }
		       });
		AlertDialog dialog = builder.create();
		dialog.show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_generate_questionaire, menu);
		return true;
	}

}
